package com.example.aicode.chat;

import com.example.aicode.chat.tools.CodeQuestionTool;
import dev.langchain4j.data.document.Document;
import dev.langchain4j.data.document.loader.FileSystemDocumentLoader;
import dev.langchain4j.data.segment.TextSegment;
import dev.langchain4j.mcp.McpToolProvider;
import dev.langchain4j.memory.ChatMemory;
import dev.langchain4j.memory.chat.MessageWindowChatMemory;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.rag.content.retriever.ContentRetriever;
import dev.langchain4j.rag.content.retriever.EmbeddingStoreContentRetriever;
import dev.langchain4j.service.AiServices;
import dev.langchain4j.store.embedding.EmbeddingStore;
import dev.langchain4j.store.embedding.EmbeddingStoreIngestor;
import dev.langchain4j.store.embedding.inmemory.InMemoryEmbeddingStore;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * 工厂实现注入
 */
@Configuration
public class AiCodeHelperServiceFactory {

    @Resource
    private ChatModel qwenChatModel;

    @Resource
    private ContentRetriever contentRetriever;

    @Resource
    private McpToolProvider mcpToolProvider;

    @Resource
    private StreamingChatModel qwenStreamingChatModel;

    @Bean
    public AiCodeHelperService createAicodeHelperService() throws Exception {
        // 基于数量10条的会话记忆，内存存储，重启丢失，可以通过ChatMemoryStore接口的实现类，将消息保存到数据库中
        ChatMemory chatMemory = MessageWindowChatMemory.withMaxMessages(10);
        // 构造AIservice
        return AiServices.builder(AiCodeHelperService.class)
                .chatModel(qwenChatModel)
                .streamingChatModel(qwenStreamingChatModel)
                .chatMemory(chatMemory) // 会话记忆
                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
                .contentRetriever(contentRetriever) //RAG 检索增强生成
                .tools(new CodeQuestionTool())
                .toolProvider(mcpToolProvider)
                .build();

        // RAG
        // 1. 加载文档
// List<Document> documents = FileSystemDocumentLoader.loadDocuments("src/main/resources/markdown");
// // 2. 使用内置的EmbeddingModel 转换文档为向量，然后存储在内存中
// EmbeddingStore<TextSegment> em =new InMemoryEmbeddingStore<>();
// EmbeddingStoreIngestor.ingest(documents,em);
// // 3.构造AI service
// return AiServices.builder(AiCodeHelperService.class)
// .chatModel(qwenChatModel)
// .chatMemory(chatMemory) //会话记忆
// .contentRetriever(EmbeddingStoreContentRetriever.from(em))
// .build();

        // 自定义会话记忆的
// ChatMemory myChatMemory = MessageWindowChatMemory.builder()
// .id("12345")
// .maxMessages(10)
// .chatMemoryStore(new PersistentChatMemoryStore())
// .build();
// return AiServices.builder(AiCodeHelperService.class)
// .chatModel(qwenChatModel)
// .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10)) //根据memoryId 会话隔离
// .chatMemory(myChatMemory) //会话记忆
// .build();

// return AiServices.create(AiCodeHelperService.class, qwenChatModel);
    }
}
